
package etcomm.SDK.HttpService;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;


public class HttpServiceWrapper
{

    private String _url;
	
    public HttpServiceWrapper(String url)
    {
        if(url.length()<1||url.charAt(url.length()-1)!='/')
            throw new IllegalArgumentException("url is error! must with '/' end");
        _url=url;
    }

 
    private String kv(String key,String value)
    {
        return String.format("%s=%s",key,java.net.URLEncoder.encode(Utility.gb2312ToUtf8(value)));
    }
    

   
    private String httpGet(String url)
    {
        String response=new String();
        HttpClient client = new HttpClient();
       
        HttpMethod method=new GetMethod(url);
        
        try{
            client.executeMethod(method);
        }catch (Exception ex)
        {
        }

        if(method.getStatusCode()==200)
        {
            try {
				response = method.getResponseBodyAsString();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
       
        method.releaseConnection();
      
        return Utility.utf8Togb2312(response);
    }

	
    public String Auth(String entAccount,String password)
    {
        return httpGet(Utility.string_Format("{0}{1}?{2}",new String[]{_url,
                "Auth.html",
                Utility.string_Join("&",new String[]{
                        kv("entAccount",entAccount),
                        kv("password",password)
                })}));
    }

	
    public String GetPowers(String token)
    {
        return httpGet(Utility.string_Format("{0}{1}?{2}", new String[]{_url,
                "GetPowers.html",
                Utility.string_Join("&", new String[]{
                        kv("token", token)
                })}));
    }

	
    public String GetEntInfo(String token)
    {
        return httpGet(Utility.string_Format("{0}{1}?{2}",new String[]{_url,
                "GetEntInfo.html",
                Utility.string_Join("&",new String[]{
                        kv("token",token)
                })}));
    }


    public String UpdateEnt(String token,String entInfo)
    {
        return httpGet(Utility.string_Format("{0}{1}?{2}",new String[]{_url,
                "UpdateEnt.html",
                Utility.string_Join("&",new String[]{
                        kv("token",token),
                        kv("entInfo",entInfo)
                })}));
    }

	
    public String AddDev(String token,String devSerial)
    {
        return httpGet(Utility.string_Format("{0}{1}?{2}",new String[]{_url,
                "AddDev.html",
                Utility.string_Join("&",new String[]{
                        kv("token",token),
                        kv("devSerial",devSerial)
                })}));
    }

	
    public String DelDev(String token,String devSerial)
    {
        return httpGet(Utility.string_Format("{0}{1}?{2}",new String[]{_url,
                "DelDev.html",
                Utility.string_Join("&",new String[]{
                        kv("token",token),
                        kv("devSerial",devSerial)
                })}));
    }

	
    public String GetDevs(String token)
    {
        return httpGet(Utility.string_Format("{0}{1}?{2}", new String[]{_url,
                "GetDevs.html",
                Utility.string_Join("&", new String[]{
                        kv("token", token)
                })}));
    }

	
    public String GetData(String token,String appType,String fromDate,String toDate,Boolean onlyUnsync,Boolean markSynced,Integer pageIndex,Integer pageSize)
    {
        return httpGet(Utility.string_Format("{0}{1}?{2}", new String[]{_url,
                "GetData.html",
                Utility.string_Join("&", new String[]{
                        kv("token", token),
                        kv("appType", appType),
                        kv("fromDate", fromDate),
                        kv("toDate", toDate),
                        kv("onlyUnsync", onlyUnsync?"on":""),
                        kv("markSynced", markSynced?"on":""),
                        kv("pageIndex",  pageIndex.toString()),
                        kv("pageSize", pageSize.toString())
                })}));
    }

}
